// import adminConfig from './admin'
import rankRouter from './rank'
import Utils from '@/lin/utils/util'

// eslint-disable-next-line import/no-mutable-exports
let homeRouter = [
  {
    title: '日志管理',
    type: 'view',
    name: Symbol('log'),
    route: '/log',
    filePath: 'views/log/Log.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '隐私设置',
    type: 'view',
    name: Symbol('system'),
    route: '/system',
    filePath: 'views/system/System.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '加入我们',
    type: 'view',
    name: Symbol('system-one'),
    route: '/system-one',
    filePath: 'views/system/SystemOne.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '店面管理',
    type: 'view',
    name: Symbol('store'),
    route: '/store',
    filePath: 'views/store/Store.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '店铺分组管理',
    type: 'view',
    name: Symbol('store-group'),
    route: '/store-group',
    filePath: 'views/store-group/StoreGroup.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '员工管理',
    type: 'view',
    name: Symbol('store-user'),
    route: '/store-user',
    filePath: 'views/store-user/StoreUser.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '团专属卡管理',
    type: 'view',
    name: Symbol('product-type'),
    route: '/product-type',
    filePath: 'views/product/ProductType.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '团管理',
    type: 'view',
    name: Symbol('team'),
    route: '/team',
    filePath: 'views/team/Team.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '团人员管理',
    type: 'view',
    name: Symbol('team-user'),
    route: '/team-staff',
    filePath: 'views/team-user/TeamUser.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '上架产品管理',
    type: 'view',
    name: Symbol('product'),
    route: '/product',
    filePath: 'views/product/Product.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '下架产品管理',
    type: 'view',
    name: Symbol('product-down'),
    route: '/product-down',
    filePath: 'views/product/ProductDown.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '用户管理',
    type: 'view',
    name: Symbol('user'),
    route: '/user',
    filePath: 'views/user/User.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '导入订单核算信息',
    type: 'view',
    name: Symbol('bus-add'),
    route: '/bus-add',
    filePath: 'views/bus/BusAdd.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  {
    title: '订单核算',
    type: 'view',
    name: Symbol('bus'),
    route: '/bus',
    filePath: 'views/bus/Bus.vue',
    inNav: true,
    icon: 'iconfont icon-rizhiguanli',
    order: null,
  },
  rankRouter,
  // adminConfig,
  {
    title: '404',
    type: 'view',
    name: Symbol('404'),
    route: '/404',
    filePath: 'views/error-page/404.vue',
    inNav: false,
    icon: 'iconfont icon-rizhiguanli',
  },
]

const plugins = []

// 筛除已经被添加的插件
function filterPlugin(data) {
  if (plugins.length === 0) {
    return
  }
  if (Array.isArray(data)) {
    data.forEach((item) => {
      filterPlugin(item)
    })
  } else {
    // eslint-disable-next-line arrow-parens
    const findResult = plugins.findIndex((item) => data === item)
    if (findResult >= 0) {
      plugins.splice(findResult, 1)
    }
    if (data.children) {
      filterPlugin(data.children)
    }
  }
}

filterPlugin(homeRouter)

homeRouter = homeRouter.concat(plugins)

// 处理顺序
homeRouter = Utils.sortByOrder(homeRouter)

// 使用 Symbol 处理 name 字段, 保证唯一性
const deepReduceName = (target) => {
  if (Array.isArray(target)) {
    target.forEach((item) => {
      if (typeof item !== 'object') {
        return
      }
      deepReduceName(item)
    })
    return
  }
  if (typeof target === 'object') {
    if (typeof target.name !== 'symbol') {
      // eslint-disable-next-line no-param-reassign
      target.name = target.name || Utils.getRandomStr()
      // eslint-disable-next-line no-param-reassign
      target.name = Symbol(target.name)
    }

    if (Array.isArray(target.children)) {
      target.children.forEach((item) => {
        if (typeof item !== 'object') {
          return
        }
        deepReduceName(item)
      })
    }
  }
}

deepReduceName(homeRouter)

export default homeRouter
